<template>
    <div class="components_edit_modal">
        <el-dialog width="500px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="film" :model="film" :rules="rules" label-width="95px" size="mini">

                <!-- <el-form-item label="用 户 名：" prop="userId">
                    <el-select v-model="film.userId" filterable remote reserve-keyword placeholder="请选择用户"
                    :remote-method="remoteMethod"
                    :loading="loading">
                        <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.login"
                            :value="item.id">
                        </el-option>
                    </el-select>
                    <el-input v-model.trim="film.userId"></el-input>
                </el-form-item> -->

                <el-form-item label="明星姓名：" prop="name">
                    <el-input v-model.trim="film.name"></el-input>
                </el-form-item>

                <el-form-item label="明星年龄：" prop="age">
                    <el-input-number v-model="film.age" :min="1" :max="200" label="请输入年龄"></el-input-number>
                </el-form-item>

                <el-form-item label="作者姓名：" prop="author">
                    <el-input v-model.trim="film.author"></el-input>
                </el-form-item>

                <el-form-item label="相册标题：" prop="albumId">
                    <el-select v-model="film.albumId" filterable placeholder="请选择相册标题或者描述">
                        <el-option
                            v-for="item in option.albumOptions"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="店铺名称：" prop="shopId">
                    <el-select v-model="film.shopId" filterable placeholder="请选择店铺店铺">
                        <el-option
                            v-for="item in option.shops"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <!--<el-form-item label="底片链接：" prop="url">
                    <el-input v-model.trim="film.url" style="display:none;"></el-input>
                    <pl-upload
                        :postData="attachment"
                        :displaymode="'images'"
                        :api="'jstn-file'"
                        :imageOnly="false"
                        :buttonIds='{select:"selectfiles1",upload:"postfiles1",container:"container1"}'
                        @reloadFiles="reloadFiles" ></pl-upload>
                </el-form-item>-->

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addfilm">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    // import PlUpload from "../../../common/PlUpload"
    export default {
        components: {
            // "pl-upload":PlUpload
        },
        data() {
            return {
                state: false,
                loading : false,
                title: '',
                type:'',
                film:{
                    id: null,
                    age: null,
                    name: null,
                    author: null,
                    albumId: null,
                    shopId: null
                },
                rules: {
                    // name: [
                    //     { required: true, message: '请输入底片名称', trigger: 'blur'}
                    // ],
                    url: [
                        { required: true, message: '请上传底片', trigger: 'blur'}
                    ],
                    userId: [
                        { required: true, message: '请选择用户', trigger: 'blur'}
                    ]
                },
                options:[],
                option:{
                    albumOptions:[],
                },
                attachment:[],//上传附件
            }
        },
        created() {},
        mounted() {
            this.getAlbumList();
            this.getShopList();
        },
        methods: {
            showDialog(val){
                if(this.$refs['film']!==undefined){
                    this.$refs['film'].resetFields();
                }
                
                if(val){
                    for(var key in this.film){
                        this.film[key] = null;
                        this.film[key] = val[key];
                    }
                    
                    /* for(var i= 0; i< val.album.photos.length;i++){
                        var obj = {};
                        val.album.photos[i].url = decodeURI(val.album.photos[i].url);
                        obj.bucket = this.urlFormat(val.album.photos[i].url).bucket;
                        obj.object = this.urlFormat(val.album.photos[i].url).objectKey;
                        obj.fileType = this.urlFormat(val.album.photos[i].url).fileType;

                        if ( $.inArray(obj.fileType, ["jpg", "jpeg","image/jpeg", "gif", "png", "bmp"]) < 0 ) {
                            obj["isImage"] = false;
                            obj["imgsrc"] = "/static/js/plupload/suffix/otherfile.png";
                        }else{
                            obj["isImage"] = true;
                            obj["imgsrc"] = obj.url;
                        }

                        obj.fileRealName = decodeURI(obj.object);
                        obj.imgsrc = val.album.photos[i].url;
                        obj.url = val.album.photos[i].url;
                        this.attachment.push(obj);
                    } */


                    this.title = "编辑每日一星";
                    this.type = "edit";
                }else{
                    this.title = "添加每日一星";
                    this.type = "add";
                }
                this.state = true;
            },
            // 获取相册数据
            getAlbumList() {
                let vm = this;
                vm.axios.get(api.picasaToolManagement.picasaToolList_No).then((response) =>{
                    if (response.data.code == 200) {
                        vm.option.albumOptions = response.data.data;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            // 获取店铺数据
            getShopList() {
                let vm = this;
                vm.axios.get(api.shopsManagement.shopsList_No).then((response) =>{
                    if (response.data.code == 200) {
                        vm.option.shops = response.data.data;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
             //  用户信息异步检索
            remoteMethod(query) {
                if (query !== '' && query.length>2 ) {
                    this.loading = true;
                    setTimeout(() => {
                        this.loading = false;
                        this.getUserList(query);
                    }, 200);
                } else {
                    this.options4 = [];
                }
            },
            // 获取用户数据
            getUserList(name) {
                let vm = this;
                vm.axios.get(api.userManagement.userList_no + "?keyWord=" + name).then((response) =>{
                    if (response.data.code == 200) {
                        vm.options = response.data.data;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            addfilm(){
                let vm = this;
                vm.$refs.film.validate((valid) => {
                    if (valid) {

                        let submission =  ''; //提交方式

                        //选择提交方式
                        if(vm.type == 'edit'){
                            submission = 'put';
                        }else{
                            submission = 'post';
                        }
                        // 提交
                        vm.axios({
                            method: submission,
                            url: api.starManagement.starList,
                            data: vm.film
                        }).then((response) => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.tableList.$emit('vuetable:refresh');
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        })
                    }else{
                        return false;
                    }
                });
            },
            reloadFiles: function (files) {
                let vm = this;
                this.attachment = [];

                files.forEach((item,idnex)=>{
                    item.url = item.fileStoreName;
                    this.attachment.push(item)
                })
                this.film.url = this.attachment[0].url;
            },
            // 格式化 url 地址
            urlFormat(strUrl){
                // var strUrl = "https://jstn-pic.oss-cn-qingdao.aliyuncs.com/d16e8272-4ac7-de6a-1071-a79ded59d29a.jpg"
                var arrUrl = strUrl.split("//")[1].split("/");
                var objectKey = arrUrl[1];
                var bucket = arrUrl[0].split(".")[0];
                var fileTypeArr = arrUrl[1].split(".");
                console.log(fileTypeArr);
                var fileType = fileTypeArr[fileTypeArr.length-1];
                var resultObject ={
                    bucket : bucket,
                    objectKey : objectKey,
                    fileType : fileType
                }
                return resultObject
            }
        },

    };
</script>
<style scoped>
.dialog-footer{ text-align:center; }
.dialog-footer button{ margin: 0 10%; }
</style>